package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.building.*;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class BuildingDaoImpl extends TempDao {

	public Map<Integer, BuildingInfo> getBuildingInfoMap() {
		Map<Integer, BuildingInfo> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_building order by id asc";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				BuildingInfo data = new BuildingInfo();
				data.setId(rs.getInt("id"));
				data.setOccupation(rs.getInt("occupation"));
				data.setPosition(rs.getInt("position"));
				data.setNeedPassId(rs.getInt("unlockPassId"));
				data.setConsume(PropertyHelper.parseStringToProperty(rs.getString("consume")));
				data.setAddStaffCost(new BigDecimal(rs.getString("addStaffCost")));
				data.setStaffEarnSpeed(new BigDecimal(rs.getString("staffEarnSpeed")));

				dataMap.put(data.getId(), data);
			}

		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}

	public Map<Integer, BuildingStaff> getBuildingStaffMap() {
		Map<Integer, BuildingStaff> dataMap = new HashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_building_staff";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());

			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				BuildingStaff data = new BuildingStaff();
				data.setConsume(new BigDecimal(rs.getString("consume")));
				data.setStaffCount(rs.getInt("staffCount"));
				dataMap.put(data.getStaffCount(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}

	public Map<Integer, Map<Integer,BuildingUpgrade>> getBuildingUpgradeMap() {
		Map<Integer, Map<Integer,BuildingUpgrade>> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_building_upgrade";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				BuildingUpgrade data = new BuildingUpgrade();
				data.setBuildingId(rs.getInt("buildId"));
				data.setLevel(rs.getInt("level"));
				data.setNeedTitleId(rs.getInt("upgradeCondition"));
				data.setConsume(PropertyHelper.parseStringToProperty(rs.getString("consume")));
				data.setEarnSpeedAddition(rs.getInt("earnSpeedAddition"));
				data.setMaxStaffCount(rs.getInt("maxStaffCount"));
				if(!dataMap.containsKey(data.getBuildingId())){
					dataMap.put(data.getBuildingId(), new ConcurrentHashMap<>());
				}
				dataMap.get(data.getBuildingId()).put(data.getLevel(), data);
			}

		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}
}